package algotithm.leetcode.math.test50;

/**
 * @author 25068
 * @Date 2021/4/26 22:51
 * @Title 50. Pow(x, n)
 */
public class Solution {

    public static void main(String[] args) {
        double x = 2.00000;
        int n = 10;
        Solution solution = new Solution();
        double res = solution.myPow(x,n);
        System.out.println(res);
    }

    public double quick(double x,long N) {
        double t = 1.0;
        while (N > 0) {
            if (N % 2 == 1) {
                t *= x;
            }
            x *= x;
            N /= 2;
        }
        return t;
    }

    public double myPow(double x, int n) {
        long N = n;
        return N > 0 ? quick(x,N) : 1.0 / quick(x, -N);
    }
}